package com.javaee.thinks.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ApiActor {
    public static List<Map<String, String>> ApiActor(String text) {
        List<Map<String, String>> commands = new ArrayList<>();
        Pattern pattern = Pattern.compile("\\{\\{\\{([^}]+)}}}"); // 匹配 {{{...}}}
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            String match = matcher.group(1); // 获取捕获组的内容
            String[] parts = match.split(",");
            String commandName = parts[0].trim();

            Map<String, String> params = new HashMap<>();
            for (int i = 1; i < parts.length; i++) {
                String[] keyValue = parts[i].split("=");
                if (keyValue.length == 2) {
                    params.put(keyValue[0].trim(), keyValue[1].trim());
                }
            }
            commands.add(Map.of("command", commandName, "params", params.toString()));
        }

        return commands;
    }
    public static void main(String[] args) {
        String text = "文本{{{cmd1,p1=v1,p2=v2}}}文本{{{cmd2,p3=v3}}}文本";
        List<Map<String,String>> result = ApiActor(text);
        System.out.println(result);
    }
}
